/**
 * @copyright 2017 sinping.com 北京芯平科技股份有限公司. All rights reserved.
 * 本内容仅限于北京芯平科技股份有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */
package com.sinping.qh.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 *
 *
 * @author guojie
 * @create 2017/12/21
 */
@Configuration
@ConditionalOnProperty(name="wn.session.cache", havingValue="redis")
public class RedisConfig {
    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.timeout}")
    private int timeout;

    @Value("${spring.redis.max-idle}")
    private int maxIdle;

    @Value("${spring.redis.max-wait}")
    private long maxWaitMillis;

    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.database}")
    private String databaseIndex;
    @Value("${spring.redis.max-active}")
    private String maxActive;
    @Bean
    public JedisPool redisPoolFactory() {
        //logger.info("JedisPool注入成功！！");
        //logger.info("redis地址：" + host + ":" + port);
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setMaxTotal(new Integer(maxActive));
        // 资源没有的时候 报错  ，默认 是true， 阻塞
        jedisPoolConfig.setBlockWhenExhausted(false);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password, Integer.valueOf(databaseIndex));
        return jedisPool;
    }
}
